package week11;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author The setting sun
 * @version 1.0
 * @title: Work4Server
 * @description: TODO
 * @date 2021/11/18 15:20
 */
public class Work4Server {
    public static void main(String[] args) {
        new MyServerFrame().init();
    }
}

class MyServerFrame {
    JFrame frame = new JFrame("服务器");

    JPanel p1 = new JPanel();
    JLabel client = new JLabel("IP地址：");
    JTextField serverIP = new JTextField(16);
    JLabel port = new JLabel("端口：");
    JTextField serverPort = new JTextField(7);
    JButton con = new JButton("开启");

    JPanel p2 = new JPanel();
    JTextArea area = new JTextArea(50, 52);
    JScrollPane sp = new JScrollPane(area);

    JPanel p3 = new JPanel();
    JTextField input = new JTextField(45);
    JButton send = new JButton("发送");

    ServerSocket ss;
    Socket socket;

    public void init() {
        ope();
        serverIP.setText("127.0.0.1");

        p1.setLayout(new FlowLayout(FlowLayout.LEFT));
        p1.add(client);
        p1.add(serverIP);
        p1.add(port);
        p1.add(serverPort);
        p1.add(con);

        p2.add(sp);

        p3.add(input);
        p3.add(send);

        frame.add(p1, BorderLayout.NORTH);
        frame.add(p2, BorderLayout.CENTER);
        frame.add(p3, BorderLayout.SOUTH);

        frame.setVisible(true);
        frame.setSize(600, 500);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void ope() {
        con.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String str = serverPort.getText();
                    Integer port = new Integer(str);
                    ss = new ServerSocket(port);
                    socket = ss.accept();
                    System.out.println("连接成功");

                    new ServerThread(socket, area).start();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        });

        send.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String text = input.getText();
                    OutputStream os = socket.getOutputStream();
                    DataOutputStream dos = new DataOutputStream(os);
                    dos.writeUTF(text);
                    input.setText("");
                    area.append("服务器：" + text + "\n");
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        });
    }
}

class ServerThread extends Thread {
    Socket socket;
    JTextArea area;

    public ServerThread(Socket socket, JTextArea area) {
        this.socket = socket;
        this.area = area;
    }

    @Override
    public void run() {
        try {
            InputStream is = socket.getInputStream();
            DataInputStream dis = new DataInputStream(is);

            while (true) {
                String text = dis.readUTF();
                area.append("客户端：" + text + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


